<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="200"
			label-align="left">
			<u-form-item label="单别" v-if="judgeShow('tg001')">
				<u-input v-model="dataForm.tg001" placeholder="单别" disabled></u-input>
			</u-form-item>
			<u-form-item label="单号" v-if="judgeShow('tg002')">
				<u-input v-model="dataForm.tg002" placeholder="单号" disabled></u-input>
			</u-form-item>
			<u-form-item label="单据日期" v-if="judgeShow('tg042')">
				<u-input v-model="dataForm.tg042" placeholder="单据日期" disabled></u-input>
			</u-form-item>
			<u-form-item label="客户编号" v-if="judgeShow('tg004')">
				<u-input v-model="dataForm.tg004" placeholder="客户编号" disabled></u-input>
			</u-form-item>
			<u-form-item label="项目编号" v-if="judgeShow('tg075')">
				<u-input v-model="dataForm.tg075" placeholder="项目编号" disabled></u-input>
			</u-form-item>
			<u-form-item label="生成分录(成本)" v-if="judgeShow('tg037')">
				<u-radio-group v-model="dataForm.tg037" :disabled="judgeWrite('tg037')">
					<u-radio v-for="(item, index) in radioList" :key="index" :name="item.id">{{ item.fullName }}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="生成分录(收入)" v-if="judgeShow('tg036')">
				<u-radio-group v-model="dataForm.tg036" :disabled="judgeWrite('tg036')">
					<u-radio v-for="(item, index) in radioList" :key="index" :name="item.id">{{ item.fullName }}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="超限放行" v-if="judgeShow('tg074')">
				<u-radio-group v-model="dataForm.tg074" :disabled="judgeWrite('tg074')">
					<u-radio v-for="(item, index) in radioList" :key="index" :name="item.id">{{ item.fullName }}
					</u-radio>
				</u-radio-group>
			</u-form-item>

			<u-tabs-swiper ref="tabs" :list="swiperList" :current="current" @change="tabsChange"
				:is-scroll="false"></u-tabs-swiper>
			<view v-show="current==0">
				<u-form-item label="业务员" v-if="judgeShow('tg006')">
					<u-input v-model="dataForm.tg006" placeholder="业务员" disabled></u-input>
				</u-form-item>
				<u-form-item label="币种" v-if="judgeShow('tg011')">
					<u-input v-model="dataForm.tg011" placeholder="币种" disabled></u-input>
				</u-form-item>
				<u-form-item label="汇率" v-if="judgeShow('tg012')">
					<u-input v-model="dataForm.tg012" placeholder="汇率" :disabled="judgeWrite('tg012')"></u-input>
				</u-form-item>
				<u-form-item label="备注" v-if="judgeShow('tg020')">
					<u-input v-model="dataForm.tg020" placeholder="备注" :disabled="judgeWrite('tg020')"></u-input>
				</u-form-item>
				<u-form-item label="部门" v-if="judgeShow('tg005')">
					<u-input v-model="dataForm.tg005" placeholder="部门" disabled></u-input>
				</u-form-item>
				<u-form-item label="打印次数" v-if="judgeShow('tg022')">
					<u-input v-model="dataForm.tg022" placeholder="打印次数" :disabled="judgeWrite('tg022')"></u-input>
				</u-form-item>
				<u-form-item label="件数" v-if="judgeShow('tg032')">
					<u-input v-model="dataForm.tg032" placeholder="件数" :disabled="judgeWrite('tg032')"></u-input>
				</u-form-item>
				<u-form-item label="销货日期" v-if="judgeShow('tg003')">
					<u-input v-model="dataForm.tg003" placeholder="销货日期" disabled></u-input>
				</u-form-item>
				<u-form-item label="出货工厂" v-if="judgeShow('tg010')">
					<u-input v-model="dataForm.tg010" placeholder="出货工厂" disabled></u-input>
				</u-form-item>
				<u-form-item label="审核者" v-if="judgeShow('tg043')">
					<u-input v-model="dataForm.tg043" placeholder="审核者" disabled></u-input>
				</u-form-item>
				<u-form-item label="付款条件编号" v-if="judgeShow('tg047')">
					<u-input v-model="dataForm.tg047" placeholder="付款条件编号" disabled></u-input>
				</u-form-item>
			</view>
			<view v-show="current==1">
				<u-form-item label="发票种类" v-if="judgeShow('tg016')">
					<u-input v-model="dataForm.tg016" placeholder="发票种类" disabled></u-input>
				</u-form-item>
				<u-form-item label="税种" v-if="judgeShow('tg017')">
					<u-input v-model="dataForm.tg017" placeholder="税种" disabled></u-input>
				</u-form-item>
				<u-form-item label="L/C_NO" v-if="judgeShow('tg039')">
					<u-input v-model="dataForm.tg039" placeholder="L/C_NO" :disabled="judgeWrite('tg039')"></u-input>
				</u-form-item>
				<u-form-item label="INVOICE_NO" v-if="judgeShow('tg040')">
					<u-input v-model="dataForm.tg040" placeholder="INVOICE_NO"
						:disabled="judgeWrite('tg040')"></u-input>
				</u-form-item>
				<u-form-item label="员工编号" v-if="judgeShow('tg035')">
					<u-input v-model="dataForm.tg035" placeholder="员工编号" disabled></u-input>
				</u-form-item>
				<u-form-item label="税率" v-if="judgeShow('tg044')">
					<u-input v-model="dataForm.tg044" placeholder="税率" :disabled="judgeWrite('tg044')"></u-input>
				</u-form-item>
				<u-form-item label="备注一" v-if="judgeShow('tg027')">
					<u-input v-model="dataForm.tg027" placeholder="备注一" :disabled="judgeWrite('tg027')"></u-input>
				</u-form-item>
				<u-form-item label="备注二" v-if="judgeShow('tg028')">
					<u-input v-model="dataForm.tg028" placeholder="备注二" :disabled="judgeWrite('tg028')"></u-input>
				</u-form-item>
				<u-form-item label="备注三" v-if="judgeShow('tg029')">
					<u-input v-model="dataForm.tg029" placeholder="备注三" :disabled="judgeWrite('tg029')"></u-input>
				</u-form-item>
				<u-form-item label="收款业务员" v-if="judgeShow('tg026')">
					<u-input v-model="dataForm.tg026" placeholder="收款业务员" disabled></u-input>
				</u-form-item>
				<u-form-item label="海关手册" v-if="judgeShow('tg066')">
					<u-input v-model="dataForm.tg066" placeholder="海关手册" :disabled="judgeWrite('tg066')"></u-input>
				</u-form-item>

			</view>

			<view class="jnpf-table" style="margin-top: 24rpx;">
				<view class="jnpf-table-item" style="margin-bottom: 24rpx;" v-for="(item,i) in dataForm.copthList"
					:key="i">
					<u-form-item label="序号" style="background-color: aqua;">
						{{i+1}}
					</u-form-item>
					<u-form-item label="订单单别">
						<u-input v-model="item.th014" placeholder="订单单别" disabled></u-input>
					</u-form-item>
					<u-form-item label="订单单号">
						<u-input v-model="item.th015" placeholder="订单单号" disabled></u-input>
					</u-form-item>
					<u-form-item label="订单序号">
						<u-input v-model="item.th016" placeholder="订单序号" disabled></u-input>
					</u-form-item>
					<u-form-item label="出货通知单别">
						<u-input v-model="item.th045" placeholder="出货通知单别" disabled></u-input>
					</u-form-item>
					<u-form-item label="出货通知单号">
						<u-input v-model="item.th046" placeholder="出货通知单号" disabled></u-input>
					</u-form-item>
					<u-form-item label="出货通知序号">
						<u-input v-model="item.th047" placeholder="出货通知序号" disabled></u-input>
					</u-form-item>
					<u-form-item label="品号">
						<u-input v-model="item.th004" placeholder="品号" disabled></u-input>
					</u-form-item>
					<u-form-item label="批号">
						<u-input v-model="item.th017" placeholder="批号" disabled></u-input>
					</u-form-item>
					<u-form-item label="越南品名">
						<u-input v-model="item.engPm" placeholder="越南品名" disabled></u-input>
					</u-form-item>
					<u-form-item label="越南规格">
						<u-input v-model="item.engGg" placeholder="越南规格" disabled></u-input>
					</u-form-item>
					<u-form-item label="品名">
						<u-input v-model="item.th005" placeholder="品名" disabled></u-input>
					</u-form-item>
					<u-form-item label="规格">
						<u-input v-model="item.th006" placeholder="规格" disabled></u-input>
					</u-form-item>

					<u-form-item label="仓库名称">
						<u-input v-model="item.th007" placeholder="仓库名称" disabled></u-input>
					</u-form-item>
					<u-form-item label="数量">
						<u-input v-model="item.th008" placeholder="数量" disabled></u-input>
					</u-form-item>
					<u-form-item label="类型">
						<u-input v-model="item.th031" placeholder="类型" disabled></u-input>
					</u-form-item>
					<u-form-item label="赠/备品量">
						<u-input v-model="item.th024" placeholder="赠/备品量" disabled></u-input>
					</u-form-item>
					<u-form-item label="单位">
						<u-input v-model="item.th009" placeholder="单位" disabled></u-input>
					</u-form-item>
					<u-form-item label="本币税前金额">
						<u-input v-model="item.th037" placeholder="本币税前金额" disabled></u-input>
					</u-form-item>
					<u-form-item label="单价">
						<u-input v-model="item.th012" placeholder="单价" disabled></u-input>
					</u-form-item>
					<u-form-item label="折扣率(销货单)">
						<u-input v-model="item.th025" placeholder="折扣率(销货单)" disabled></u-input>
					</u-form-item>
					<u-form-item label="金额">
						<u-input v-model="item.th013" placeholder="金额" disabled></u-input>
					</u-form-item>
					<u-form-item label="原币税前金额">
						<u-input v-model="item.th035" placeholder="原币税前金额" disabled></u-input>
					</u-form-item>
					<u-form-item label="原币税额">
						<u-input v-model="item.th036" placeholder="原币税额" disabled></u-input>
					</u-form-item>

					<u-form-item label="本币税额">
						<u-input v-model="item.th038" placeholder="本币税额" disabled></u-input>
					</u-form-item>

					<u-form-item label="批号说明">
						<u-input v-model="item.th055" placeholder="批号说明" disabled></u-input>
					</u-form-item>
					<u-form-item label="生产日期">
						<u-input v-model="item.th051" placeholder="生产日期" disabled></u-input>
					</u-form-item>
					<u-form-item label="有效日期">
						<u-input v-model="item.th052" placeholder="有效日期" disabled></u-input>
					</u-form-item>
					<u-form-item label="复检日期">
						<u-input v-model="item.th053" placeholder="复检日期" disabled></u-input>
					</u-form-item>
					<u-form-item label="客户品号">
						<u-input v-model="item.th019" placeholder="客户品号" disabled></u-input>
					</u-form-item>
					<u-form-item label="客户规格">
						<u-input v-model="item.mg005" placeholder="客户规格" disabled></u-input>
					</u-form-item>
					<u-form-item label="备注">
						<u-input v-model="item.th018" placeholder="备注" disabled></u-input>
					</u-form-item>
					<u-form-item label="开票码">
						<u-radio-group v-model="item.th026" :disabled="judgeWrite('tg036')">
							<u-radio v-for="(item, index) in radioList" :key="index" :name="item.id">{{ item.fullName }}
							</u-radio>
						</u-radio-group>
					</u-form-item>
					<u-form-item label="已开票数量">
						<u-input v-model="item.th042" placeholder="已开票数量" disabled></u-input>
					</u-form-item>
					<u-form-item label="借出单别">
						<u-input v-model="item.th032" placeholder="借出单别" disabled></u-input>
					</u-form-item>
					<u-form-item label="借出单号">
						<u-input v-model="item.th033" placeholder="借出单号" disabled></u-input>
					</u-form-item>

					<u-form-item label="借出序号">
						<u-input v-model="item.th034" placeholder="借出序号" disabled></u-input>
					</u-form-item>
					<u-form-item label="包材品号">
						<u-input v-model="item.udf01" placeholder="包材品号" disabled></u-input>
					</u-form-item>
					<u-form-item label="包材品名">
						<u-input v-model="item.udf02" placeholder="包材品名" disabled></u-input>
					</u-form-item>
					<u-form-item label="包材数量">
						<u-input v-model="item.udf51" placeholder="包材数量" disabled></u-input>
					</u-form-item>
				</view>

			</view>
			<u-form-item label="原币销货金额">
				<u-input v-model="dataForm.tg013" placeholder="原币销货金额" disabled></u-input>
			</u-form-item>
			<u-form-item label="原币销货税额">
				<u-input v-model="dataForm.tg025" placeholder="原币销货税额" disabled></u-input>
			</u-form-item>
			<u-form-item label="原币合计">
				<u-input v-model="dataForm.sum" placeholder="原币合计" disabled></u-input>
			</u-form-item>

			<u-form-item label="总数量">
				<u-input v-model="dataForm.tg033" placeholder="总数量" disabled></u-input>
			</u-form-item>
			<u-form-item label="本币销货金额">
				<u-input v-model="dataForm.tg045" placeholder="本币销货金额" disabled></u-input>
			</u-form-item>
			<u-form-item label="本币销货税额">
				<u-input v-model="dataForm.tg046" placeholder="本币销货税额" disabled></u-input>
			</u-form-item>
			<u-form-item label="本币合计">
				<u-input v-model="dataForm.sumLocalCurrency" placeholder="本币合计" disabled></u-input>
			</u-form-item>
			<u-form-item label="附件">
				<jnpf-file v-model="dataForm.attachmentUpload" />
			</u-form-item>
		</u-form>
	</view>
</template>

<script>
	import comMixin from '../mixin'
	import {
		getGoodsList,
		getCustomerList,
		Info,
		Create,
		Update,
	} from '@/api/apply/order'
	import {
		getCoptg
	} from '@/api/workFlow/flowBefore'
	import {
		getDictionaryDataSelector,
		getDataInterfaceDataInfo,

	} from '@/api/common'
	export default {
		mixins: [comMixin],
		watch: {
			"dataForm.copthList": {
				handler(newVal, oldVal) {
					var copthList = newVal;
					this.calculatePrice(copthList);
				},
				deep: true,
			},
		},
		data() {
			return {
				current: 0,
				totalNums: 0, //总件数
				totalPrice: 0, //总件数
				buyName: '',
				categoriesOfTaxesName: '',
				categoriesOfTaxesLst: [],
				tel: '', //联系人
				telTwo: '', //联系人
				hide: false,
				billEnCode: 'OrderNumber',
				dataForm: {},
				infoObj: {},
				lst: [],
				swiperList: [{
					name: '交易信息'
				}, {
					name: '其他信息'
				}],
				dictionaryInfo: {},
				rules: {
					customerId: [{
						required: true,
						message: '客户名称不能为空',
						trigger: 'input'
					}],
					salesmanId: [{
						required: true,
						message: '业务人员不能为空',
						trigger: 'input'
					}],
					orderDate: [{
						required: true,
						message: '订单日期不能为空',
						trigger: 'change',
						type: 'number'
					}],
					paymentMode: [{
						required: true,
						message: '付款方式不能为空',
						trigger: 'input'
					}]
				},
				options: [{
					id: '现金',
					fullName: '现金'
				}, {
					id: '转帐',
					fullName: '转帐'
				}, {
					id: '汇票',
					fullName: '汇票'
				}],
				info: {},
				formConf: {},
				listInfo: {
					optionOne: '',
					optionTwo: '',
					optionThree: '',
					optionFour: '',
					JoinStr: '',
					gochang: '',
				},
				id: '',
				formOperatesList: [],
				radioList: [{
						fullName: 'Y',
						id: 'Y'
					},
					{
						fullName: 'N',
						id: 'N'
					},
				],
			}
		},
		methods: {
			init(data, id, flowFormContentJson) {
				this.setting = data
				this.formOperatesList = data.formOperates
				this.id = id
				this.getInfoFn(id)
			},
			getInfoFn(id) {
				getCoptg(id).then(res => {
					if (res.code == 200) {
						console.log(res.data);
						this.dataForm = res.data

						for (let i = 0; i < this.dataForm.copthList.length; i++) {
							this.getDataInterfaceDatack(this.dataForm.copthList[i].th007, i)
						}
						this.getDataInterfaceDatatg004(this.dataForm.tg004)
						this.getDataInterfaceDatatg011(this.dataForm.tg011)
						this.getDictionaryData()
					}
				})
			},
			//汇总到单头
			calculatePrice(copthList) {
				let money1 = 0; //原币销货金额
				let money2 = 0; //原币销货税额
				let money3 = 0; //原币合计
				let money4 = 0; //本币销货金额
				let money5 = 0; //本币销货税额
				let money6 = 0; //本币合计
				let money7 = 0; //总数量
				for (let i = 0; i < copthList.length; i++) {
					const e = copthList[i];
					money1 += parseFloat(e.th035); //税前金额
					money2 += parseFloat(e.th036); //税额
					money3 = money1 + money2; //合计
					money4 += parseFloat(e.th037); //本币税前金额
					money5 += parseFloat(e.th038); //本币税额
					money6 = money4 + money5; //本币金额
					money7 = money7 + parseFloat(e.th008) + parseFloat(e.th024); //
				}
				this.dataForm.tg013 = parseFloat(money1).toFixed(2);
				this.dataForm.tg025 = parseFloat(money2).toFixed(2);
				this.dataForm.sum = parseFloat(money3).toFixed(2);
				this.dataForm.tg045 = parseFloat(money4).toFixed(2);
				this.dataForm.tg046 = parseFloat(money5).toFixed(2);
				this.dataForm.sumLocalCurrency = parseFloat(money6).toFixed(2);
				this.dataForm.tg033 = parseFloat(money7).toFixed(2);
			},
			getDataInterfaceDatatg004(id) {
				let data = {
					id,
					interfaceId: '362118781926906437',
					propsValue: 'MA001',
					relationField: 'MA002',
				}
				getDataInterfaceDataInfo('362118781926906437', data).then(res => {
					if (res.code == 200) {
						this.dataForm.tg004 = res.data.MA002
					}
				})
			},
			getDataInterfaceDatatg011(id) {
				let data = {
					id,
					interfaceId: '362115609221143109',
					propsValue: 'MF001',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo('362115609221143109', data).then(res => {
					if (res.code == 200) {
						this.dataForm.tg011 = res.data.JoinStr
					}
				})
			},
			getDataInterfaceDatack(id, index) {
				let data = {
					id,
					interfaceId: '362114335423601221',
					propsValue: 'MC001',
					relationField: 'MC001',
				}
				getDataInterfaceDataInfo('362114335423601221', data).then(res => {

					if (res.code == 200) {
						this.dataForm.copthList[index].th007 = res.data.MC002
					}
				})
			},
			getDictionaryData() {
				getDictionaryDataSelector('413956176477180421').then(res => {
					this.dataForm.tg016 = res.data.list.find(item => item.enCode === this.dataForm.tg016)
						?.fullName;
				})
				getDictionaryDataSelector('	411695330707391429').then(res => {
					this.dataForm.tg017 = res.data.list.find(item => item.enCode === this.dataForm.tg017)
						?.fullName;
				})
			},
			tabsChange(e) {
				this.current = e
			},
			selfGetInfo() {
				Info(this.setting.id).then(res => {
					this.dataForm = res.data
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson)
					}
				})
			},
			selfSubmit() {
				this.dataForm.status = this.eventType === 'submit' ? 0 : 1
				if (this.eventType === 'save') return this.selfHandleRequest()
				uni.showModal({
					title: '提示',
					content: '您确定要提交当前流程吗？',
					success: res => {
						if (res.confirm) {
							this.selfHandleRequest()
						}
					}
				})
			},
			selfHandleRequest() {
				if (!this.dataForm.id) delete(this.dataForm.id)
				const formMethod = this.dataForm.id ? Update : Create
				formMethod(this.dataForm).then(res => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit('refresh')
								uni.navigateBack()
							}, 1500)
						}
					})
				})
			},
			initData() {
				getGoodsList().then(res => {
					this.goodsOptions = res.data.list
				})
				getCustomerList().then(res => {
					this.customerOptions = res.data.list
				})
			},
			selfInit() {
				this.addGoods()
				this.addPlan()
			},
			onChange(e) {
				const user = e[0]
				this.dataForm.salesmanName = user.fullName
			},
			addGoods() {
				const item = {
					goodsId: '',
					goodsCode: '',
					goodsName: '',
					specifications: '',
					unit: '',
					qty: '',
					price: '',
					amount: '',
					discount: null,
					cess: null,
					actualPrice: '',
					actualAmount: '',
					description: ''
				}
				this.dataForm.goodsList.push(item)
			},
			delGoods(index) {
				this.dataForm.goodsList.splice(index, 1)
			},
			addPlan() {
				const item = {
					receivableDate: '',
					receivableRate: '',
					receivableMoney: '',
					receivableMode: '',
					abstract: ''
				}
				this.dataForm.collectionPlanList.push(item)
			},
			delPlan(index) {
				this.dataForm.collectionPlanList.splice(index, 1)
			},
			onGoodsChange(e, i) {
				const goods = e[0]
				this.dataForm.goodsList[i] = {
					goodsId: goods.id,
					goodsCode: goods.code,
					goodsName: goods.text,
					specifications: goods.specifications,
					unit: goods.unit,
					qty: 1,
					price: goods.price,
					amount: goods.price,
					discount: 100,
					cess: 0,
					actualPrice: goods.price,
					actualAmount: goods.price,
					description: ''
				}
				this.$forceUpdate()
			},
			onCustomerChange(e) {
				const customer = e[0]
				this.dataForm.customerName = customer.text
			},
			count(row) {
				//金额 = 数量*单价
				row.amount = this.jnpf.toDecimal(parseFloat(row.price) * parseFloat(row.qty))
				//折扣价 = (单价*折扣)
				var discountPrice = row.price * (row.discount / 100);
				//实际单价 = 折扣价 * (1 + (税率 / 100))
				row.actualPrice = this.jnpf.toDecimal(discountPrice * (1 + (row.cess / 100)));
				//实际金额
				row.actualAmount = this.jnpf.toDecimal(parseFloat(row.actualPrice) * parseFloat(row
					.qty))
				this.$forceUpdate()
			}
		}
	}
</script>